<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title></title>
        <script type="text/javascript" src="jquery.js"></script>
        <style>
            body { overflow: hidden; margin: 0px;padding: 0px; background-color: white; }
            div:first-child { margin-top: 0px; }
            div { cursor: pointer; text-align: left; padding: 0px 0px; font-family: sans-serif;font-size: 0.8em;width: 100px;margin-top: 2px;background: transparent;}
			a {text-decoration:none;};
            div:hover {background: #f2f2f2;text-decoration:none;}
			#download {width:200px;height:20px;text-align:center;padding:2px 2px;}
			#processind {position:absolute;width:150px;height:12px;border:1px solid #000000;display:inline;}
			#bar {float:left;width:0%;height:12px;background-color:#00FF00;}
        </style>
        <script type="text/javascript">
			var ajax = null;		
			var pattern = "";
			var server ="";
			var service = "";
			var hostname = "";	
			function callAjax(data){
				if (window.XMLHttpRequest) {
					ajax = new XMLHttpRequest();
					var parameters = "url=" + data.url + "&hq=" + data.quality + "&server="+ server+ "&lang="+data.lang + "&adblock=" + data.adblock;
					update_time = new Date().getTime();
					ajax.open("POST", "http://www.video2mp3.net/check.php", true);
					ajax.onreadystatechange = refresh_ajax;
					ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
					ajax.setRequestHeader("Content-length", parameters.length);
					ajax.setRequestHeader("Connection", "close");
					ajax.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
					ajax.send(parameters);
					$('#download').css('text-align','left');
					$('#download').html('Please, wait...');
				}
			};
			
			function refresh_ajax() {	
				if (ajax.readyState == 4) {       
					var response = ajax.responseText.split("|");
					if(ajax.status == 404) {   
						
					}	 
					if((ajax.status != 200 || ajax.responseText=="" || (response[0] == "MESSAGE" && response[4]!="ok")) && pattern!="" && service!="") {	
						ajax.open("GET", "http://www.video2mp3.net/update.php?v=" + pattern + "&service=" + service + "&hostname=" + hostname, true);		
						ajax.onreadystatechange = refresh_ajax;
						ajax.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');		
						setTimeout('ajax.send(null)', 1000);	 
					}	 
					else if(response[0] == "ERROR") {
						
					}	 
					else if (response[0] == "WAIT") {	 
						var countdown_num = parseInt(parseInt(response[1])*10);
						var countdown_total = parseInt(parseInt(response[1])*10);
						var downloaduri = response[5].replace(/^.*YouTube/i,'http://www.video2mp3.net/load/YouTube');
						console.log('source: ',response[5]);
						console.log('target: ',downloaduri);
						chrome.extension.getBackgroundPage().start({downloaduri:downloaduri,countdown_num:countdown_num,countdown_total:countdown_total},function(res){
							window.close();
						});
					}	 
				}
			}
			
			function download(){
				var background = chrome.extension.getBackgroundPage();
				background.download(function(res){		
					if (res.status=='OK'){
						callAjax(res.data);
					}
				});
            };

			
        </script>
    </head>
    <body> 
        <div onclick="download()" id="download">download</div>
    </body>
</html>
